package com.nmsl.membership.controller;


import com.alibaba.fastjson2.JSONObject;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.R;
import com.nmsl.membership.domain.ValueStorage;

import com.nmsl.membership.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
@CrossOrigin
@RestController
@RequestMapping("/membership/recharge")
public class RechargeRecordController{

    @Autowired
    private RechargeRecordService rechargeRecordService;

    @PostMapping("/add")
    public R<String> addRecharge(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String zengjiaJine = jsonObject.getString("zengjiaJine");
        Integer patientId = jsonObject.getInteger("patientId");
        if (patientId == null || zengjiaJine == null || zengjiaJine.equals("0") || patientId == 0) {
            return R.fail("0");

//|| Integer.getInteger(zengjiaJine).intValue()<=0
        }else {
            try {
                rechargeRecordService.addRechargeRecord(zengjiaJine, patientId);
                System.out.println("111111111111111111111111111111111111111111111111111111111111111111111111111111111");
                return R.ok("1");
            } catch (Exception e) {
                return R.fail("0"+ e.getMessage());
            }
        }

    }
    @PostMapping("/less")
    public ResponseEntity<String> LessRecharge(
            @RequestParam("jianshaoJine") String jianshaoJine,
            @RequestParam("patientId") Integer patientId) {


        if (patientId == null || jianshaoJine == null || jianshaoJine.equals("0")  || patientId == 0) {
//|| Integer.getInteger(jianshaoJine).intValue()<0

            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("0");
        }
        try {
            rechargeRecordService.LessRechargeRecord(jianshaoJine, patientId);
            return ResponseEntity.ok("1");
        }catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("0: " + e.getMessage());
        }
    }


            //用于微信小程序
    @PutMapping("/less2")
    public ResponseEntity<String> LessRecharge2(@RequestBody String params) {
            JSONObject jsonObject = JSONObject.parseObject(params);
            String jianshaoJine = jsonObject.getString("jianshaoJine");
            Integer patientId = jsonObject.getInteger("patientId");

        if (patientId == null || jianshaoJine == null || jianshaoJine.equals("0")  || patientId == 0) {
//|| Integer.getInteger(jianshaoJine).intValue()<0

            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("0");
        }
        try {
            rechargeRecordService.LessRechargeRecord(jianshaoJine, patientId);
            return ResponseEntity.ok("1");
        }catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("0: " + e.getMessage());
        }
    }


}
